package com.lh.java.javamail;

import java.io.File;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.apache.struts2.ServletActionContext;

/**
 * 发送邮件 类
 * 
 * @author Gu Weiqing
 * 
 */
public class MailUtil {

	private String stmp;
	public String pop3;
	private boolean isAuth;
	private String email;
	private String password;

	public MailUtil(String stmp, String pop3, boolean isAuth, String email, String password) {
		this.stmp = stmp;
		this.pop3 = pop3;
		this.isAuth = isAuth;
		this.email = email;
		this.password = password;
	}

	public void saveSendEmailInfo(String sendEmail, String receEmail, String title) {
		// 记录 发送信息

	}

	public void sendMail(String subject, String content, String... recipients) throws Exception {
		Properties props = new Properties();
		props.put("mail.smtp.host", this.stmp);
		props.put("mail.smtp.auth", this.isAuth ? "true" : "false");
		Session session = Session.getDefaultInstance(props, null);
		session.setDebug(true);
		MimeMessage message = new MimeMessage(session);
		message.setSubject(subject);
		message.setText(content);
		Address from = new InternetAddress(this.email);
		Address to = new InternetAddress(recipients[0]);
		message.setFrom(from);
		message.setRecipient(Message.RecipientType.TO, to);
		Transport transport = session.getTransport("smtp");
		transport.connect(this.stmp, this.email, this.password);
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();

		saveSendEmailInfo(this.email, recipients[0], subject);
	}

	public void sendMailForHtml(String subject, String content, String... recipients) throws MessagingException   {
		Properties props = new Properties();
		props.put("mail.smtp.host", this.stmp);
		props.put("mail.smtp.auth", this.isAuth ? "true" : "false");
		Session session = Session.getDefaultInstance(props, null);
		session.setDebug(true);
		MimeMessage message = new MimeMessage(session);
		message.setSubject(subject);
		message.setContent(
				"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" /></head>" + content
						+ "</html>", "text/html; charset=utf-8");
		Address from = new InternetAddress(this.email);
		Address to = new InternetAddress(recipients[0]);
		message.setFrom(from);
		message.setRecipient(Message.RecipientType.TO, to);
		Transport transport = session.getTransport("smtp");
		try {
			transport.connect(this.stmp, this.email, this.password);
		} catch (MessagingException e) {
			System.out.println("ha ha,et  iskfdjksdjf;");
			e.printStackTrace();
		}
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();

		saveSendEmailInfo(this.email, recipients[0], subject);
	}

	public void sendMailForAttachment(String subject, String content, String attachmentNewName, String... recipients)
			throws Exception {
		Properties props = new Properties();
		props.put("mail.smtp.host", this.stmp);
		props.put("mail.smtp.auth", this.isAuth ? "true" : "false");
		Session session = Session.getDefaultInstance(props, null);
		session.setDebug(true);

		MimeMessage message = new MimeMessage(session);
		message.setSubject(subject);
		// message.setContent("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" /></head>"+content+"</html>","text/html; charset=utf-8");

		Address from = new InternetAddress(this.email);
		Address to = new InternetAddress(recipients[0]);

		message.setFrom(from);
		message.setRecipient(Message.RecipientType.TO, to);

		// create the message part
		MimeBodyPart messageBodyPart = new MimeBodyPart();

		// fill message
		messageBodyPart.setContent(
				"<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" /></head>" + content
						+ "</html>", "text/html; charset=utf-8");

		Multipart multipart = new MimeMultipart();
		multipart.addBodyPart(messageBodyPart);

		// Part two is attachment //附件
		if (attachmentNewName != null && !attachmentNewName.equals("")) {
			String attachmentPath1 = ServletActionContext.getServletContext().getRealPath("") + attachmentNewName;
			File attachmentFile1 = new File(attachmentPath1);
			if (attachmentFile1.exists()) {
				messageBodyPart = new MimeBodyPart();
				DataSource source = new FileDataSource(attachmentPath1);
				messageBodyPart.setDataHandler(new DataHandler(source));
				messageBodyPart.setFileName(MimeUtility.encodeWord(attachmentNewName, "GB2312", null));
				multipart.addBodyPart(messageBodyPart);
			}
		}

		// Put parts in message
		message.setContent(multipart);
		message.saveChanges();

		Transport transport = session.getTransport("smtp");
		transport.connect(this.stmp, this.email, this.password);
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();

		saveSendEmailInfo(this.email, recipients[0], subject);
	}

	public static void main(String args[]) {
		/*
		 * MailUtil mailUtil = new
		 * MailUtil("smtp.gmail.com","pop3.gmail.com",true
		 * ,"dev@winclik.net","developer@998"); try {
		 * mailUtil.sendMailForHtml("密码重置",
		 * "您好,<br />因为你忘记了密码,并请求重置密码.如果你没有请求,请忽略.24小时后这个请求将会过期不可用.<br />要重置你的密码,请访问以下页面:<br /><a \n href='http://www.wiclick.cn/userhandler/lostPwd.do?action=retakePwd&id=&random='>http://www.wiclick.cn/userhandler/lostPwd.do?action=retakePwd&amp;id=&amp;random=</a>"
		 * ,"atuo912@163.com"); } catch (Exception e) { e.printStackTrace(); }
		
		 */
		MailUtil mu=new MailUtil("smtp.163.com","sadf.pop3.163.com",true,"lihenghunan@163.com","wabbmm555lh");
		try {
			mu.sendMailForHtml("你好lhdsf", "test", "529016080@qq.com");
		} catch (Exception e) {			
			e.printStackTrace();
		}

	//	System.out.println("abcsdfasdfas".indexOf("http://"));
	}

}
